let App = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [],
    coupons: {},
    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '您还没有相关的优惠券'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(option) {
    let userId = wx.getStorageSync('userId')
    let navList = [{
      name: '未使用',
      id: '0',
    }, {
      name: '使用中',
      id: '1',
    }, {
      name: '已使用',
      id: '2',
    }]
    this.setData({
      userId,
      navList,
      activeIndex: this.data.activeIndex,
      'coupons.params.status': parseInt(navList[this.data.activeIndex].id),
    })
    // this.onReachBottom()
    wx.showNavigationBarLoading();
    this.initData();
    this.getList(() => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // wx.showNavigationBarLoading();
    // this.initData();
    // this.getList(() => {
    //   wx.hideNavigationBarLoading();
    //   wx.stopPullDownRefresh();
    // })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
  // 初始化数据
  initData() {
    let {
      coupons,
      userId
    } = this.data;
    let params = coupons && coupons.params;
    let status = parseInt(params.status);
    this.setData({
      coupons: {
        items: [],
        total: '',
        params: {
          userId: userId,
          // page: 1,
          // pageCount: 5,
          status: status,
        }
      }
    })
  },
  // 获取优惠券列表
  getList(cb) {
    let coupons = this.data.coupons
    let params = coupons.params
    App.HttpService.dzq(params).then(res => {
      cb && cb()
      let data = res.data
      if (data.code == 200) {
        coupons.items = [...coupons.items, ...data.data.couponsList]
        if (data.data.couponsList.length > 0) {
          params.page = params.page + 1
          coupons.total = data.data.counts
        }
        this.setData({
          coupons: coupons,
          'prompt.hidden': coupons.items.length,
        })
      }
    }, e => {
      cb && cb()
    })
  },
  // 切换tabbar
  onTapTag(e) {
    let type = e.currentTarget.dataset.type
    let index = e.currentTarget.dataset.index
    this.initData()
    this.setData({
      activeIndex: index,
      'coupons.params.status': parseInt(type),
    })
    this.getList()
  },
  jinru: function(e) {
    var type = e.currentTarget.dataset.type
    // 10 普通
    // 11 洗车
    // 12 服务
    console.log(e.currentTarget.dataset.id)
    var s = e.currentTarget.dataset.id
    var d = this.data.activeIndex
 

     
     if(d==0){
    var pagem = {
      // 折扣
      zhekou: e.currentTarget.dataset.discount,
      // 电子券id
      dzqid: e.currentTarget.dataset.id,
    }
    var papa = JSON.stringify(pagem)
    var shu = 1
    if (type == 11) {
      wx.navigateTo({
        url: '/pages/wash-car/list/list?zk=' + shu + "&pag=" + papa,
      })
      return
    } else if (type == 12) {
      var pak = {
        // 折扣
        zhekou: e.currentTarget.dataset.discount,
        // 电子券id
        dzqid: e.currentTarget.dataset.id,
        qian: e.currentTarget.dataset.qian,
      }
      var pas = JSON.stringify(pak)
      wx.navigateTo({
        url: '/pages/merchants/lists/index?s_id=' + s + "&can=" + pas
      })
      return
    } else if (type == 10) {
    
      var pak = {
        // 折扣
        zhekou: e.currentTarget.dataset.discount,
        // 电子券id
        dzqid: e.currentTarget.dataset.id,
        qian: e.currentTarget.dataset.qian,
      }
      var canshu = JSON.stringify(pak)
      wx.navigateTo({
        url: '/pages/product/package/index?can=' + canshu
      })
      return
    }
    } else {
      wx.showToast({
        title: '已使用',
        image: '../../assets/images/warning.png',
        duration: 1500
      })
    }

  }
})